<template>
  <base-breadcrumb>
    <searchData
      ref="searchData"
      :search-data="searchArr"
      @search="search"
    />
    <a-card>
      <list-table
        ref="table"
        class="export-table"
        :columns="columns"
        :startIndex="0"
        :request="loadData"
        :export-config="{ use: true, select: '表扬信管理', all: '表扬信管理', customColumns: customColumns }"
        :export-permission="null"
      >
        <template slot="prefix">
          <a-button
            type="primary"
            @click="handleEdit(null, 1)"
            v-btnPermission="'crm_007_add_01'"
          >新增</a-button>
        </template>
        <!-- 操作 -->
        <template
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
            <a
              v-btnPermission="'crm_007_loo_02'"
              @click="handleEdit(record, 3)"
            >查看</a>
            <a
              v-btnPermission="'crm_007_edi_03'"
              @click="() => handleEdit(record, 2)"
            >编辑</a>
            <a
              v-btnPermission="'crm_007_del_04'"
              @click="handleSure(record.id)"
            >删除</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>
<script>
import { getPageCustomerPraise, deleteByPrimaryKey } from '@/api/complaint/satisfactionsurvey.js'
import SelectProject from '@/components/Select/SelectProject' // 项目名称
import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject' // 组织
import OrgTreeSelect from '@/components/OrgTreeSelect'
const columns = [
  { title: '所属分公司', dataIndex: 'branchComName', width: 160 },
  { title: '项目名称', dataIndex: 'projectName', width: 220 },
  { title: '表扬单位', dataIndex: 'praiseCompany', width: 160 },
  { title: '创建时间', dataIndex: 'createTime', width: 160 },
  { title: '表扬信获得时间', dataIndex: 'gainTime', width: 160 },
  { title: '操作', dataIndex: 'operation', width: 200, fixed: 'right', scopedSlots: { customRender: 'operation' } }
]
export default {
  name: 'a' + Date.now(),
  data: function () {
    this.columns = columns
    this.customColumns = columns.slice(0, 6)
    let exportArr = [
      { title: '表扬事项', dataIndex: 'reason', width: 200 },
      { title: '创建人', dataIndex: 'createUserName', width: 200 }
    ]
    this.customColumns = [...this.customColumns, ...exportArr]
    return {
      loadData: (parameter) => {
        let that = this
        parameter.pageNum = parameter.pageNo
        delete parameter.pageNo
        const requestParameters = Object.assign({}, parameter, { param: that.queryParam })
        return getPageCustomerPraise(requestParameters).then((res) => {
          return { code: 200, data: res.data?.list, totalSize: res.data?.total }
        })
      },
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
      ownerData: [],
      loading: false,
      pathName: this.$route.path,
      queryParam: {},
      pageNo: 1,
      pageSize: 10,
      totalSize: 0,
      searchArr: [
          {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
          attrs: {
            showOther: true
          }
        },
        // {
        //   name: '所属组织',
        //   valueKey: 'statisticsId',
        //   type: SelectCompanyAndProject
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   keyValue: 'branchComId',
        //   valueKey: 'projectId'
        // },
        {
          name: '表扬单位',
          value: 'praiseCompany',
          type: 'input'
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'dateRange',
          attr: {
            rangeFormat: ['startTime', 'endTime']
          }
        },
        {
          name: '表扬信获得时间',
          value: 'gainTime',
          type: 'dateRange',
          attr: {
            rangeFormat: ['gainStartTime', 'gainEndTime']
          }
        }
      ],
      selectedRowKeys: [],
      selectId: [],
      selectedRows: [],
      chooseData: 0,
      tableData: [],
      searchData: {}
    }
  },
  created() {
    //this.list()
  },
  watch: {
    $route(to, from) {
      if (to.params?.statisticsId) {
        this.$nextTick(() => {
          from.path === '/complaint/letter' && this.$refs.searchData?.form?.resetFields()
          const data = this.$route.params
          this.searchData = {
            gainStartTime: (data?.gainTime || [])[0],
            gainEndTime: (data?.gainTime || [])[1],
            statisticsId: data?.statisticsId
          }
          this.$refs.searchData?.form?.resetFields()
          this.$refs.searchData.form.setFieldsValue(data)
          this.search(this.searchData)
        })
      } else {
        // this.$refs.searchData?.form?.resetFields()
        // this.$refs.searchData.form?.setFieldsValue()
        // this.search()
      }
    }
  },
  mounted: function () {
    if (this.$route.params) {
      const data = this.$route.params
      this.searchData = {
        gainStartTime: (data?.gainTime || [])[0],
        gainEndTime: (data?.gainTime || [])[1],
        statisticsId: data?.statisticsId
      }
      this.$refs.searchData?.form?.resetFields()
      this.$refs.searchData.form?.setFieldsValue(data)
      this.search(this.searchData)
    }
    this.$bus.$on('handleRefresh' + this.pathName, () => {
      //this.list()
    })
  },
  beforeDestroy() {
    this.$bus.$off('handleRefresh' + this.pathName)
  },
  methods: {
    async search(values) {
      if (values?.gainStartTime && values?.gainStartTime === 'Invalid date') {
        values.gainStartTime = this.searchData.gainStartTime
      }
      if (values?.gainEndTime && values?.gainEndTime === 'Invalid date') {
        values.gainEndTime = this.searchData.gainEndTime
      }
      this.queryParam = { ...values }
      await this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      let _this = this
      _this.selectId = []
      selectedRows.map((item) => {
        _this.selectId.push(item.id)
      })
      this.chooseData = selectedRowKeys.length
      _this.selectedRowKeys = selectedRowKeys
      _this.selectedRows = selectedRows
    },
    handleSure(id) {
      let that = this
      that.$confirm({
        title: '温馨提示',
        content: '是否删除?',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          return deleteByPrimaryKey(id)
            .then((res) => {
              that.$message.success('删除成功！')
              that.$refs.table.refresh(true)
            })
            .catch((err) => {
              console.log(err)
            })
        }
      })
    },
    handleEdit(record, state) {
      this.$router.push({
        name: 'AddLetter',
        query: {
          id: (record && record.id) || '',
          state: state
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.searchTable {
  .form_flex {
    display: flex;
    justify-content: space-between;
  }
}
</style>
